package com.evil.application.enums.process;

import com.evil.common.activiti.enums.HandleTypeEnum;
import com.evil.common.activiti.enums.NodeTypeEnum;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 订单节点流转类型
 *
 * @author 15587
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum CompleteTypeEnum {

    /**
     * 订单节点流转类型
     */
    PASS(1, "通过"),
    FALLBACK(2, "回退"),
    REFUSE(3, "拒绝"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public HandleTypeEnum getHandleTypeEnum(NodeTypeEnum nodeTypeEnum) {
        switch (this) {
            case PASS:
                if (NodeTypeEnum.APPROVAL_NODE.getId() == nodeTypeEnum.getId()) {
                    return HandleTypeEnum.APPROVED;
                } else {
                    return HandleTypeEnum.SUBMIT;
                }
            case REFUSE:
                return HandleTypeEnum.REJECTED;
            case FALLBACK:
                return HandleTypeEnum.FALLBACK;
            default:
                throw new BusinessException(RCodeEnum.ERROR_RUNTIME);
        }
    }

    public static Optional<CompleteTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static CompleteTypeEnum isExistById(Integer id) {
        return CompleteTypeEnum.findById(id).orElseThrow(() ->
                new BusinessException(RCodeEnum.ERROR_PARAMETER, "流转类型参数错误"));
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "流转类型参数错误");
    }
}
